package com.example.job.Activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.bumptech.glide.Glide
import com.example.job.Model.JobModel
import com.example.job.R
import com.example.job.databinding.ActivityJobDetailBinding
import com.example.job.db.userdb
import com.example.job.entity.UserInfo
import com.example.job.fragment.JobFragment
import com.example.job.fragment.JobInfoFragment

class JobDetailActivity : AppCompatActivity() {
    lateinit var binding: ActivityJobDetailBinding
    private lateinit var item: JobModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityJobDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

        getBundle()
        setUpViewPager()
    }

    private fun getBundle() {
        item = intent.getParcelableExtra("object")!!
        binding.title.text = item.title
        binding.company.text = item.company
        binding.edu.text = item.edu
        binding.experience.text = item.experience
        binding.people.text = item.people
        binding.salary.text = "${item.minsalary}-${item.maxsalary}"
        binding.city.text = item.city

        Glide.with(this).load(item.img).into(binding.logo)

        binding.backbtn.setOnClickListener {
            finish()
        }

        // 收藏
        binding.collect.setOnClickListener {
            val userId = UserInfo.getUserInfo(this)?.user_id
            val jobId = item.id
            println("User ID: $userId, Job ID: $jobId")

            if (userId == null || jobId == null) {
                Log.e("syj", "User ID or Job ID is null")
                return@setOnClickListener
            }

            var  updateJobId = userdb.getInstance(this).updateJobId(userId, jobId.toString())
            // 发送广播通知数据更新
            sendBroadcast(Intent("com.example.job.UPDATE_DATA"))
            Log.d("syj", "Broadcast sent to update data")

            if(updateJobId.toInt() == 0){
                Toast.makeText(this, "已收藏", Toast.LENGTH_SHORT).show()
            }else{
                Toast.makeText(this, "收藏成功", Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }

    private fun setUpViewPager() {
        val adapter = ViewPageAdapter(supportFragmentManager)
        val tab1 = JobFragment()
        val tab2 = JobInfoFragment()

        val bundle1 = Bundle()
        bundle1.putString("tag", item.tag)
        bundle1.putString("welfare", item.welfare)

        tab1.arguments = bundle1
        tab2.arguments = Bundle()

        adapter.addFragment(tab1, "职位描述")
        adapter.addFragment(tab2, "职位要求")

        binding.viewpager.adapter = adapter
        binding.toolbar.setupWithViewPager(binding.viewpager)
    }

    private class ViewPageAdapter(fragment: FragmentManager) : FragmentPagerAdapter(fragment) {
        private val fragmentList = arrayListOf<Fragment>()
        private val fragmentTitleList = arrayListOf<String>()

        override fun getCount(): Int = fragmentList.size
        override fun getItem(position: Int): Fragment = fragmentList[position]
        fun addFragment(fragment: Fragment, title: String) {
            fragmentList.add(fragment)
            fragmentTitleList.add(title)
        }

        override fun getPageTitle(position: Int): CharSequence = fragmentTitleList[position]
    }
}